﻿using System;
using System.Collections.Generic;

namespace DigitalQuran.Statistic
{
    public class WordStatisticsTable : List<WordStatistic>, IComparer<WordStatistic>
    {
        public static SortMethod SortMethod;
        public static SortOrder SortOrder;

        /// <summary>
        /// Compare two instances of WordStatistic
        /// </summary>
        /// <param name="x">WordStatistic to compare with</param>
        /// <param name="y">WordStatistic to compare</param>
        /// <returns>Returns difference between two WortStatistic</returns>
        public int Compare(WordStatistic x, WordStatistic y)
        {
            if (SortOrder == SortOrder.Ascending)
            {
                if (SortMethod == SortMethod.ByOrder)                
                    return x.Order.CompareTo(y.Order);                
                else if (SortMethod == SortMethod.ByLetter)                
                    return x.Word.ToString(Globals.DefaultTextMode).CompareTo(y.Word.ToString(Globals.DefaultTextMode));                
                else if (SortMethod == SortMethod.ByFrequency)
                {
                    if (x.Frequency.CompareTo(y.Frequency) == 0)
                        return x.Order.CompareTo(y.Order);
                    
                    return x.Frequency.CompareTo(y.Frequency);
                }
                else
                {
                    if (x.Frequency.CompareTo(y.Frequency) == 0)                    
                        return x.Order.CompareTo(y.Order);                    
                    return x.Frequency.CompareTo(y.Frequency);
                }
            }
            else
            {
                if (SortMethod == SortMethod.ByOrder)                
                    return y.Order.CompareTo(x.Order);                
                else if (SortMethod == SortMethod.ByLetter)                
                    return y.Word.ToString(Globals.DefaultTextMode).CompareTo(x.Word.ToString(Globals.DefaultTextMode));                
                else if (SortMethod == SortMethod.ByFrequency)
                {
                    if (y.Frequency.CompareTo(x.Frequency) == 0)                    
                        return y.Order.CompareTo(x.Order);
                    
                    return y.Frequency.CompareTo(x.Frequency);
                }
                else
                {
                    if (y.Frequency.CompareTo(x.Frequency) == 0)                    
                        return y.Order.CompareTo(x.Order);
                    
                    return y.Frequency.CompareTo(x.Frequency);
                }
            }
        }
    }
}